import requests

# 发送get请求
url = "http://127.0.0.1:8787/coupApply/cms/goodsList"
header = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
req_data = {
    "msgType": "getHandsetListOfCust",
    "page": 1,
    "size": 20
}
res = requests.get(url, params=req_data, headers=header)
# 返回状态码
# print(res.status_code)
# # 返回文本格式
# print(res.text)
# # 返回json格式
# print(res.json())

# 发送post请求 - 表单
url_login = "http://127.0.0.1:8787/dar/user/login"
header_login = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
data_login = {
    "user_name": "test01",
    "passwd": "admin123"
}
res_login = requests.post(url_login, data=data_login)
# 返回状态码
# print(res_login.status_code)
# # 返回文本格式
# print(res_login.text)
# # 返回json格式
# print(res_login.json())

# 发送post请求 - json
url_detail = "http://127.0.0.1:8787/coupApply/cms/productDetail"
header_detail = {"Content-Type": "application/json;charset=UTF-8"}
data_detail = {
    "pro_id": "33809635011",
    "page": 1,
    "size": 20
}
res_detail = requests.post(url_detail, json=data_detail, headers=header_detail)
# 返回状态码
# print(res_detail.status_code)
# # 返回文本格式
# print(res_detail.text)
# # 返回json格式
# print(res_detail.json())

# 会话对象session，使用会话对象可以保持连接状态
# 创建session
session = requests.Session()
# 发送get请求
res = session.get(url=url, params=req_data)
# print(res.text)

# 发送post请求
res_1 = session.post(url=url_detail, json=data_detail, headers=header_detail)
# print(res_1.text)
res_2 = session.post(url=url_login, data=data_login, headers=header_login)
# print(res_1.text)

# 直接调用request方法发起接口请求
method = "post"
res = requests.request(method=method, url=url_detail, json=data_detail, headers=header_detail)
print(res.text)
